package com.longtop.perform.ar.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.BeanUtil;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;
import com.longtop.intelliweb.view.Record;
import com.longtop.perform.ar.model.TArDgAcct;
import com.longtop.perform.ar.model.TArDgAcctMx;
import com.longtop.perform.ar.service.AssignMasterService;
import com.longtop.perform.ar.service.AssignMxService;
import com.longtop.perform.ar.service.AssignService;

/**
 * 绩效分配--客户分配
 * 
 * @author 杨向阳
 * 
 */
public class ArDgAcctAction extends BaseAction {
	private final static String Dataset_Master = "Dataset_Master";
	private final static String Dataset_Detail = "Dataset_Detail";

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 获取页面上的dataset的结构
		Dataset dataset = context.getDataset(Dataset_Master);
		String acctId = context.getRequest().getParameter("acctId");
		String hql = "FROM TArDgAcct WHERE resId='" + acctId + "' ORDER BY startDate";
		// 调用Spring的业务逻辑类
		List list = null;
		// 调用Spring的业务逻辑类
		AssignService assignService = this.getBean(AssignService.BEAN_ID);
		list = assignService.queryAll(hql);
		BeanUtil.listToDataset(list, dataset, false);

	}

	/**
	 * 保存、修改、删除一起做！
	 * 
	 * @param context
	 */
	public void saveMasterDetail(IActionContext context) {

		Dataset dataset = context.getDataset(Dataset_Master); // 获取页面上的dataset的结构
		Dataset datasetDetail = context.getDataset(Dataset_Detail); // 获取页面上的dataset的结构

		try {
			Record record = dataset.getCurrentRecord();
			// 把dataset转成要删除的主键列表、新增bean及更新bean列表
			Map changeMap = WebUtil.datasetToObjectList(dataset, "id", TArDgAcct.class);
			SessionObject sessionObject = this.getSessionObject(context);
			String assignId = "";
			if (changeMap != null) {
				// 保存分配主表：
				AssignMasterService assignMasterService = this.getBean(AssignMasterService.BEAN_ID);
				String assignIdinsert = assignMasterService.saveMaster(changeMap, sessionObject);
				if ("".equals(assignId)) {
					assignId = assignIdinsert;
				}
			}

			Map changeMxMap = WebUtil.datasetToObjectList(datasetDetail, "id", TArDgAcctMx.class);
			if (changeMxMap != null) {
				// 保存分配子表
				AssignMxService assignDetailerService = this.getBean(AssignMxService.BEAN_ID);
				assignDetailerService.saveDetail(changeMxMap, sessionObject, assignId, TArDgAcct.class);
			}

			this.hintInfoMessage("保存成功。");
		} catch (Exception e) {
			this.hintErrorMessage(e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * 查询子表信息
	 * 
	 * 
	 */
	public void getDetail(IActionContext context) {

		Dataset dataset = this.getDataset(context, Dataset_Detail);
		AssignService assignService = this.getBean(AssignService.BEAN_ID);

		// 获取dataset,验证数据集对象是否存在
		if (!WebUtil.validateDatasetExist(this, dataset, Dataset_Detail)) {
			return;
		}
		// 查询条件设值
		HashMap hashMap = new HashMap();
		// 判断是否是由 init方法 调用
		String fkId = context.getCommandParameter("fkId").getValue();
		hashMap.put("tablenames", "T_AR_DG_ACCT_MX");
		hashMap.put("fkId", fkId);

		List list = assignService.loadAssignAll("arAssignSql.selectDetailList", hashMap);
		BeanUtil.listToDataset(list, dataset, false);
	}
}